/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.troop.entity;

import java.util.ArrayList;
import org.joda.time.DateTime;
import org.joda.time.Duration;

/**
 *
 * @author gustavo
 */
public class TroopDetail {

    private String id; // Id is made by: detailType+troopOwnerVillageId+x+y
    private Integer villageId;
    private String detailType;
    private String troopHeadline;
    private Integer troopOwnerVillageId;
    private String troopOwner;
    private Integer x;
    private Integer y;
    private ArrayList<Integer> troops;
    private Duration arrivalDuration;
    private DateTime arrivalDate;

    public TroopDetail() {
        troops = new ArrayList<Integer>(11);
        arrivalDate = DateTime.now();
    }

    @Override
    public boolean equals(Object obj) {
        boolean equals = false;

        if (obj == null) {
            return equals;
        }
        TroopDetail detail = (TroopDetail) obj;

        //if both are null is true, if both aren't null and are the same, is true.
        boolean equalsTroopOwnerVillageId = this.troopOwnerVillageId == null && detail.getTroopOwnerVillageId() == null;
        equalsTroopOwnerVillageId = equalsTroopOwnerVillageId ? equalsTroopOwnerVillageId : this.troopOwnerVillageId.equals(detail.getTroopOwnerVillageId());
        
        
        
        boolean equalsDetailType = this.detailType == null && detail.getDetailType() == null;
        equalsDetailType = equalsDetailType ? equalsTroopOwnerVillageId : this.detailType.equals(detail.getDetailType());
        
        
        boolean equalsX = this.x == null && detail.getX() == null;
        equalsX = equalsX ? equalsX : this.x.equals(detail.getX());
        
        boolean equalsY = this.y == null && detail.getY() == null;
        equalsY = equalsY ? equalsY : this.y.equals(detail.getY());
        
        equals = equalsDetailType && equalsTroopOwnerVillageId && equalsX && equalsY;
        return equals;
    }

    public void createAndSetId() {
        String newId = "";
        newId += detailType == null ? "null" : detailType;
        newId += troopOwnerVillageId == null ? "null" : troopOwnerVillageId;
        newId += x == null ? "null" : x;
        newId += y == null ? "null" : y;
        this.id = newId;
    }
    
    public String getId(){
        return this.id;
    }

    public String getDetailType() {
        return detailType;
    }

    public void setDetailType(String detailType) {
        this.detailType = detailType;
    }

    public String getTroopHeadline() {
        return troopHeadline;
    }

    public void setTroopHeadline(String troopHeadline) {
        this.troopHeadline = troopHeadline;
    }

    public String getTroopOwner() {
        return troopOwner;
    }

    public void setTroopOwner(String troopOwner) {
        this.troopOwner = troopOwner;
    }

    public ArrayList<Integer> getTroops() {
        return troops;
    }

    public void setTroops(ArrayList<Integer> troops) {
        this.troops = troops;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

    public Integer getTroopOwnerVillageId() {
        return troopOwnerVillageId;
    }

    public void setTroopOwnerVillageId(Integer troopOwnerVillageId) {
        this.troopOwnerVillageId = troopOwnerVillageId;
    }

    public DateTime getArrivalDate() {
        return arrivalDate;
    }

    public void setArrivalDate(DateTime arrivalDate) {
        this.arrivalDate = arrivalDate;
    }

    public Duration getArrivalDuration() {
        return arrivalDuration;
    }

    public void setArrivalDuration(Duration arrivalDuration) {
        this.arrivalDuration = arrivalDuration;
        setArrivalDate(arrivalDuration);
    }

    public void setArrivalDuration(Long duration) {
        arrivalDuration = new Duration(duration);
        setArrivalDate(arrivalDuration);
    }

    /**
     * 
     * 
     */
    public void setArrivalDuration(String hhmmss) {

        if (hhmmss.length() == 6) {
            Integer hours = Integer.parseInt(hhmmss.substring(0, 2));
            Integer mins = Integer.parseInt(hhmmss.substring(2, 4));
            Integer secs = Integer.parseInt(hhmmss.substring(4, 6));

            arrivalDuration = new Duration(hours * 3600000 + mins * 60000 + secs * 1000);
            setArrivalDate(arrivalDuration);

        }

    }

    private void setArrivalDate(Duration duration) {
        arrivalDate = arrivalDate.plus(duration);

    }

    public Integer getVillageId() {
        return villageId;
    }

    public void setVillageId(Integer villageId) {
        this.villageId = villageId;
    }
    
    
}
